#include <REGX52.H>


//数码管段选
unsigned char NixieTable[]={
														0x7B,0x60,0xAB,0xEA,0xF0,0xDA,0xDB,0x68,0xFB,0xFA,
														0x7F,0x64,0xAF,0xEE,0xF4,0xDE,0xDF,0x6C,0xFF,0xFE,
														0x00,0x04,0x80,0x84};
/*
显示数组：
0-0，1-1，2-2，3-3，4-4，5-5，6-6，7-7，8-8，9-9
10-0.，11-1.，12-2.，13-3.，14-4.，15-5.，16-6.，17-7.，18-8.，19-9.，
清屏：20，只有一点：21，负号：22，负号有一点：23
*/

//数码管赋值
unsigned char Nixie_Buf[5]={0,0,0,0,0};

//OneWire引脚定义
sbit OneWire_DQ=P2^0;

//单总线初始化
unsigned char OneWire_Init(void)
{
	unsigned char i;
	unsigned char AckBit;
	EA=0;
	OneWire_DQ=1;
	OneWire_DQ=0;
	i = 247;while (--i);		
	OneWire_DQ=1;
	i = 32;while (--i);			
	AckBit=OneWire_DQ;
	i = 247;while (--i);		
	EA=1;
	return AckBit;
}

//单总线发送一位
void OneWire_SendBit(unsigned char Bit)
{
	unsigned char i;
	EA=0;
	OneWire_DQ=0;
	i = 4;while (--i);			
	OneWire_DQ=Bit;
	i = 24;while (--i);			
	OneWire_DQ=1;
	EA=1;
}

//单总线接收一位
unsigned char OneWire_ReceiveBit(void)
{
	unsigned char i;
	unsigned char Bit;
	EA=0;
	OneWire_DQ=0;
	i = 2;while (--i);			
	OneWire_DQ=1;
	i = 2;while (--i);			
	Bit=OneWire_DQ;
	i = 24;while (--i);			
	EA=1;
	return Bit;
}

//单总线发送一个字节
void OneWire_SendByte(unsigned char Byte)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		OneWire_SendBit(Byte&(0x01<<i));
	}
}

//单总线接收一个字节
unsigned char OneWire_ReceiveByte(void)
{
	unsigned char i;
	unsigned char Byte=0x00;
	for(i=0;i<8;i++)
	{
		if(OneWire_ReceiveBit()){Byte|=(0x01<<i);}
	}
	return Byte;
}



//DS18B20指令
#define DS18B20_SKIP_ROM			0xCC
#define DS18B20_CONVERT_T			0x44
#define DS18B20_READ_SCRATCHPAD 	0xBE

//开始温度变换
void DS18B20_ConvertT(void)
{
	OneWire_Init();
	OneWire_SendByte(DS18B20_SKIP_ROM);
	OneWire_SendByte(DS18B20_CONVERT_T);
}

//DS18B20读取温度
float DS18B20_ReadT(void)
{
	unsigned char TLSB,TMSB;
	int Temp;
	float T;
	OneWire_Init();
	OneWire_SendByte(DS18B20_SKIP_ROM);
	OneWire_SendByte(DS18B20_READ_SCRATCHPAD);
	TLSB=OneWire_ReceiveByte();
	TMSB=OneWire_ReceiveByte();
	Temp=(TMSB<<8)|TLSB;
	T=Temp/16.0;
	return T;
}

//延时
void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

//变量
float T,TShow,TSet,TSetShow;
int Goldenglow=1;
int ShowOrSet=1;


void main()
{
	DS18B20_ConvertT();	
	Delay(1000);
	
	TMOD &= 0xF0;		
	TMOD |= 0x01;		
	TL0 = 0x18;		
	TH0 = 0xFC;		
	TF0 = 0;		
	TR0 = 1;		
	ET0=1;
	EA=1;
	PT0=0;
	
	TSet=30;
	
	while(1)
	{

		//状态转换按键
		if(P3_5==0 && P3_6==0)
		{
			Delay(20);
			while(P3_5==0 && P3_6==0);
			Delay(20);
			ShowOrSet = -ShowOrSet;
		}
		if(ShowOrSet==-1)
		{
			if(P3_5==0 && P3_6!=0)
			{
				Delay(20);
				while(P3_5==0 && P3_6!=0);
				Delay(20);
				TSet=TSet+0.1;
			}
			if(P3_6==0 && P3_5!=0)
			{
				Delay(20);
				while(P3_6==0 && P3_5!=0);
				Delay(20);
				TSet=TSet-0.1;
			}
		}
		//温度读取及显示
		DS18B20_ConvertT();	
		T=DS18B20_ReadT();	
		if(T<0)				
		{
			if(ShowOrSet==1)Nixie_Buf[4]=22;
			TShow=-T;		
		}
		else				
		{
			if(ShowOrSet==1)Nixie_Buf[4]=20;
			TShow=T;
		}
		
		//设定温度显示
		if(TSet<0)			
		{
			if(ShowOrSet==-1)
			{
				if(Goldenglow==-1)Nixie_Buf[4]=22;
				if(Goldenglow==1)Nixie_Buf[4]=23;
			}
			TSetShow=-TSet;	
		}
		else				
		{
			if(ShowOrSet==-1)
			{
				if(Goldenglow==-1)Nixie_Buf[4]=20;
				if(Goldenglow==1)Nixie_Buf[4]=21;
			}
			TSetShow=TSet;
		}
		
		if(ShowOrSet==1)
		{
			Nixie_Buf[3]=	(unsigned int)(TShow) /10%10;
			Nixie_Buf[2]=	((unsigned int)(TShow) /1%10)+10;
			Nixie_Buf[1]=	(unsigned int)(TShow*10)%10;	
		}
		else
		{
			Nixie_Buf[3]= (unsigned int)(TSetShow) /10%10;
			Nixie_Buf[2]= ((unsigned int)(TSetShow) /1%10)+10;		
			Nixie_Buf[1]= (unsigned int)(TSetShow*10)%10;
		}
		//报警
		if(T>=TSet){P2_1=0;}
		else{P2_1=1;}

	}
}
		

void Nixie_Scan(unsigned char Location,Number)//单字节显示
{
	P1=0x00;
	switch(Location)	
	{
		case 1:P2_4=0;P2_5=1;P2_6=1;P2_7=1;break;
		case 2:P2_4=1;P2_5=0;P2_6=1;P2_7=1;break;
    case 3:P2_4=1;P2_5=1;P2_6=0;P2_7=1;break;
    case 4:P2_4=1;P2_5=1;P2_6=1;P2_7=0;break;
    case 5:P2_4=1;P2_5=1;P2_6=1;P2_7=1;break;			
	}
	P1=NixieTable[Number];	
}



void Timer0_Routine() interrupt 1 //定时
{
	static unsigned char i=1;
	static unsigned int T0Count1,T0Count2;
	TL0 = 0x18;		//设定定时初值
	TH0 = 0xFC;		//设定定时初值
	
	//数码管
	T0Count1++;
	if(T0Count1>=1)
	{
		T0Count1=0;
		Nixie_Scan(i,Nixie_Buf[i]);
		i++;
		if(i>=6){i=1;}	
	}
	
		T0Count2++;
	if(T0Count2>=400)
	{
		T0Count2=0;
		Goldenglow=-Goldenglow;
	}
	
}